package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysYongyouCgdd;
import com.ruoyi.system.service.ISysYongyouCgddService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购订单Controller
 * 
 * @author SYF
 * @date 2025-05-09
 */
@RestController
@RequestMapping("/system/yycgdd")
public class SysYongyouCgddController extends BaseController {

    @Autowired
    private ISysYongyouCgddService sysYongyouCgddService;

    /**
     * 查询采购订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysYongyouCgdd sysYongyouCgdd) {
        if(sysYongyouCgdd.getCode()!=null){
            if(sysYongyouCgdd.getCode().replaceAll(" ","").length()>0){
                sysYongyouCgdd.setCode(sysYongyouCgdd.getCode().replaceAll(" ",""));
            }
        }
        if (sysYongyouCgdd.getOrgName()!=null){
            if(sysYongyouCgdd.getOrgName().replaceAll(" ","").length()>0){
                sysYongyouCgdd.setOrgName(sysYongyouCgdd.getOrgName().replaceAll(" ",""));
            }
        }
        if(sysYongyouCgdd.getVendorName()!=null) {
            if (sysYongyouCgdd.getVendorName().replaceAll(" ", "").length() > 0) {
                sysYongyouCgdd.setVendorName(sysYongyouCgdd.getVendorName().replaceAll(" ",""));
            }
        }
        if(sysYongyouCgdd.getProductCcode()!=null) {
            if (sysYongyouCgdd.getProductCcode().replaceAll(" ", "").length() > 0) {
                sysYongyouCgdd.setProductCcode(sysYongyouCgdd.getProductCcode().replaceAll(" ",""));
            }
        }
        if(sysYongyouCgdd.getProductCname()!=null) {
            if (sysYongyouCgdd.getProductCname().replaceAll(" ", "").length() > 0) {
                sysYongyouCgdd.setProductCname(sysYongyouCgdd.getProductCname().replaceAll(" ",""));
            }
        }
        startPage();
        List<SysYongyouCgdd> list = sysYongyouCgddService.selectSysYongyouCgddList(sysYongyouCgdd);
        if(list.size()>0){
            for (SysYongyouCgdd sysYongyouCgdd1 : list) {
                sysYongyouCgdd1.setVouchdate(sysYongyouCgdd1.getVouchdate().substring(0, 10));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出采购订单列表
     */
    @Log(title = "采购订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysYongyouCgdd sysYongyouCgdd)
    {
        List<SysYongyouCgdd> list = sysYongyouCgddService.selectSysYongyouCgddList(sysYongyouCgdd);
        if(list.size()>0){
            for (SysYongyouCgdd sysYongyouCgdd1 : list) {
                sysYongyouCgdd1.setVouchdate(sysYongyouCgdd1.getVouchdate().substring(0, 10));
            }
        }
        ExcelUtil<SysYongyouCgdd> util = new ExcelUtil<SysYongyouCgdd>(SysYongyouCgdd.class);
        util.exportExcel(response, list, "采购订单数据");
    }

    /**
     * 获取采购订单详细信息
     */
    @GetMapping(value = "/{vouchdate}")
    public AjaxResult getInfo(@PathVariable("vouchdate") String vouchdate)
    {
        return success(sysYongyouCgddService.selectSysYongyouCgddByVouchdate(vouchdate));
    }

    /**
     * 新增采购订单
     */
    @Log(title = "采购订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysYongyouCgdd sysYongyouCgdd)
    {
        return toAjax(sysYongyouCgddService.insertSysYongyouCgdd(sysYongyouCgdd));
    }

    /**
     * 修改采购订单
     */
    @Log(title = "采购订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysYongyouCgdd sysYongyouCgdd)
    {
        return toAjax(sysYongyouCgddService.updateSysYongyouCgdd(sysYongyouCgdd));
    }

    /**
     * 删除采购订单
     */
    @Log(title = "采购订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{vouchdates}")
    public AjaxResult remove(@PathVariable String[] vouchdates)
    {
        return toAjax(sysYongyouCgddService.deleteSysYongyouCgddByVouchdates(vouchdates));
    }
}
